﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Core.Web;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using FineUIPro;


namespace CanDoo.Oil.WebApp.buyInStore
{
    /// <summary>
    /// DetailData 的摘要说明
    /// </summary>
    public class DetailData : HttpHandleBase<IInStoreDetailContract, InStoreDetail>
    {

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            long id = Web.Request.QueryString("Id").ToLong();
            JArray ja = new JArray();

            DB.KeepReading(() => {
                var totalNum = 0.0;
                var totalMoney = 0.0;
                var totalAlreadyNum = 0.0;
                var totalAlreadyMoney = 0.0;
                var entityList = Manager.Entities.Where(p => p.InStoreId == id).ToList();
                JArray jaItem = new JArray();
                foreach (var m in entityList)
                {
                    jaItem = new JArray();
                    jaItem.Add(m.Product.Code);
                    jaItem.Add(m.Product.Name);
                    jaItem.Add(m.Product.SN);
                    jaItem.Add(m.Product.Model);
                    jaItem.Add(m.Product.Specifications);
                    jaItem.Add(m.Product.ProductUnit.Name);
                    jaItem.Add(m.Price);
                    jaItem.Add(m.Num);
                    jaItem.Add(m.Money);
                    jaItem.Add(m.AlreadyNum);
                    jaItem.Add(m.AlreadyMoney);

                    totalNum += m.Num;
                    totalMoney += m.Money;
                    totalAlreadyNum += m.AlreadyNum;
                    totalAlreadyMoney += m.AlreadyMoney;
                    ja.Add(jaItem);
                }

                //合计行
                jaItem = new JArray();
                jaItem.Add("<span style='color:red;font-weight:bold;'>合计</span>");
                jaItem.Add("");
                jaItem.Add("");
                jaItem.Add("");
                jaItem.Add("");
                jaItem.Add("");
                jaItem.Add("");
                jaItem.Add("<span style='color:red;font-weight:bold;'>" + totalNum + "</span>");
                jaItem.Add("<span style='color:red;font-weight:bold;'>" + totalMoney + "</span>");
                jaItem.Add("<span style='color:red;font-weight:bold;'>" + totalAlreadyNum + "</span>");
                jaItem.Add("<span style='color:red;font-weight:bold;'>" + totalAlreadyMoney + "</span>");
                ja.Add(jaItem);
            }
            );

            context.Response.ContentType = "text/plain";
            context.Response.Write(ja.ToString(Newtonsoft.Json.Formatting.None));
        }
    }
}